var express = require('express');
var router = express.Router();
const db = require("../database/db");
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


// 注册接口
router.post('/register', function(req, res, next) {
  let{cellphone, password, nickname} = req.body;

  let sql = `INSERT INTO logon (cellphone, password, nickname) VALUES (?, ?, ?)`;
  db.query(sql, [cellphone, password, nickname], function(err, result) {
    if (err){
      console.log(err);
      res.json({code: 400, msg: "注册失败" });
      
    }else{
      res.json({code: 200, msg: "注册成功", data: result });
    }
  })
})
// 登录接口
router.post('/logon', function(req, res, next) {
  let{username, password} = req.body;
  let sql = `SELECT * FROM logon WHERE nickname=? AND password=?`;
  db.query(sql, [username, password], function(err, result) {
    if (result.length == 0){
      res.json({ code: 400, msg: "登录失败" });
    }else{
      if (err) {
        console.log(err);
        res.json({ code: 400, msg: "登录失败" });
      } else {
        res.json({
          code: 200,
          msg: "登录成功",
          data: result,
        });
      }
    }
      
  })
})
module.exports = router;
